package com.ghost.alibabaghost.service.sys;

import com.ghost.alibabaghost.api.model.sys.GhostDictModel;
import com.ghost.alibabaghost.api.service.sys.GhostDictService;
import com.ghost.alibabaghost.dao.dataobject.sys.GhostDictDO;
import com.ghost.alibabaghost.dao.mapper.sys.GhostDictMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2022/1/24 06:42
 */
@Service("ghostDictService")
public class GhostDictServiceImpl implements GhostDictService {

    @Autowired
    private GhostDictMapper ghostDictMapper;

    @Override
    public List<GhostDictModel> queryByParentCode(String parentCode) {
        return ghostDictMapper.queryByParentCode(parentCode).stream().map(model -> {
            GhostDictModel ghostDictModel = new GhostDictModel();
            BeanUtils.copyProperties(model, ghostDictModel);
            return ghostDictModel;
        }).collect(Collectors.toList());
    }

    @Override
    public Integer insertDict(GhostDictModel ghostDictModel) {
        GhostDictDO ghostDictDO = new GhostDictDO();
        BeanUtils.copyProperties(ghostDictModel, ghostDictDO);
        return ghostDictMapper.insertDict(ghostDictDO);
    }

    @Override
    public Integer deleteByDictCode(Integer id) {
        return ghostDictMapper.deleteByDictCode(id);
    }

    @Override
    public List<GhostDictModel> getTreeList(String parentCode) {
        List<GhostDictDO> treeList = ghostDictMapper.getTreeList(parentCode);
        return treeList.stream().map(ghostDictDO -> {
            GhostDictModel dictModel = new GhostDictModel();
            BeanUtils.copyProperties(ghostDictDO, dictModel);
            return dictModel;
        }).collect(Collectors.toList());
    }
}
